<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: coupon_admin.php 30 2013-09-21 11:54:26Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 30 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:54:26 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
require(DIR_WS_CLASSES.'currencies.php');
$currencies = new currencies();
  
require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.phpmailer.php');
require_once(DIR_FS_INC.'inc.php_mail.php');

$smarty = new Smarty;
if ($_GET['selected_box']) {
	$_GET['action'] = '';
	$_GET['old_action'] = '';
}

if (($_GET['action'] == 'send_email_to_user') && ($_POST['customers_email_address']) && (!$_POST['back_x'])) {

	if (is_numeric($_POST['customers_email_address'])) {
		$mail = $db->db_query("SELECT 
									customers_firstname, 
									customers_lastname, 
									customers_email_address, 
									customers_id 
								FROM 
									".TABLE_CUSTOMERS." 
								WHERE 
									customers_status = '".$_POST['customers_email_address']."'");
	
		$customers_status = $db->db_query("SELECT 
												customers_status_id, 
												customers_status_name 
											FROM 
												".TABLE_CUSTOMERS_STATUS." 
											WHERE 
												language_id = '".$_SESSION['languages_id']."' 
											AND 
												customers_status_id = '".$_POST['customers_email_address']."'");

		$mail_sent_to = $customers_status->fields['customers_status_name'];
		
	} else {
		switch ($_POST['customers_email_address']) {
			// An alle Kunden versenden
		    case '***':
				$mail = $db->db_query("SELECT 	
											customers_firstname, 
											customers_lastname, 
											customers_email_address, 
											customers_id 
										FROM 
											".TABLE_CUSTOMERS);
				$mail_sent_to = TEXT_ALL_CUSTOMERS;
				break;

			// An Newsletter Abonenten versenden
		    case '**D':
				$mail = $db->db_query("SELECT 
											customers_firstname, 
											customers_lastname, 
											customers_email_address, 
											customers_id 
										FROM 
											".TABLE_CUSTOMERS." 
										WHERE 
											customers_newsletter = '1'");
				$mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
				break;
			
			// An einen einzelnen Kunden versenden
		    default:
				$mail = $db->db_query("SELECT 
											customers_firstname, 
											customers_lastname, 
											customers_email_address, 
											customers_id 
										FROM 
											".TABLE_CUSTOMERS." 
										WHERE 
											customers_email_address = ".$db->db_prepare($_POST['customers_email_address']));
				$mail_sent_to = $_POST['customers_email_address'];
				break;
		}
	}

    $coupon_result = $db->db_query("SELECT 	
		    							coupon_code, 
		    							coupon_type, 
		    							coupon_minimum_order, 
		    							coupon_amount, 
		    							coupon_start_date, 
		    							coupon_expire_date, 
		    							restrict_to_products, 
		    							restrict_to_categories 
		    						FROM 
		    							".TABLE_COUPONS." 
		    						WHERE 
		    							coupon_id = '".$_GET['cid']."'");

    $coupon_name = $db->db_query("SELECT 
    									coupon_name 
    								FROM 
    									".TABLE_COUPONS_DESCRIPTION." 
    								WHERE 
    									coupon_id = '".(int)$_GET['cid']."' 
    								AND 
    									language_id = '".(int)$_SESSION['languages_id']."'");

    $subject = $_POST['subject'];
	
	require_once (DIR_FS_INC.'inc.get_mail_body.php');
	require_once (DIR_FS_INC.'inc.get_mail_data.php');
	
    while (!$mail->EOF) {

		$smarty->assign('language', $_SESSION['language']);
		$smarty->caching = false;

		$smarty->template_dir = DIR_FS_CATALOG.'templates';
		$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';
		$smarty->config_dir = DIR_FS_CATALOG.'lang';
		$smarty->assign('tpl_path','templates/'.CURRENT_TEMPLATE.'/');
		$smarty->assign('logo_path', HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_IMAGES);

		$smarty->assign('MESSAGE', $_POST['message_gv']);
		$smarty->assign('NAME', $mail->fields['customers_firstname'].' '.$mail->fields['customers_lastname']);
		$smarty->assign('COUPON_ID', $coupon_result->fields['coupon_code']);
		$smarty->assign('COUPON_TYPE', $coupon_result->fields['coupon_type']);
		
   	  	$link = HTTP_SERVER.DIR_WS_CATALOG.'gv_redeem.php?gv_no='.$coupon_result->fields['coupon_code'];
		$smarty->assign('GIFT_LINK', $link);
		$smarty->assign('WEBSITE', HTTP_SERVER .DIR_WS_CATALOG);
		
		if ($coupon_result->fields['coupon_type'] == 'P')
			$smarty->assign('COUPON_AMOUNT', round($coupon_result->fields['coupon_amount'], 2).' %');
		else if ($coupon_result->fields['coupon_type'] == 'F')
		  	$smarty->assign('COUPON_AMOUNT', $currencies->format($coupon_result->fields['coupon_amount']));
		else if ($coupon_result->fields['coupon_type'] == 'S')
		  	$smarty->assign('COUPON_AMOUNT', TEXT_FREE_SHIPPING);
		else if ($coupon_result->fields['coupon_type'] == 'G')
		  	$smarty->assign('COUPON_AMOUNT', $currencies->format($coupon_result->fields['coupon_amount']));

		if ($coupon_result->fields['coupon_minimum_order'] != "")
			$smarty->assign('COUPON_MINIMUM_ORDER', $currencies->format($coupon_result->fields['coupon_minimum_order']));
		
		if ($coupon_result->fields['restrict_to_products'] != "")
			$smarty->assign('COUPON_RESTRICT_TO_PRODUCTS', $coupon_result->fields['restrict_to_products']);

		if ($coupon_result->fields['restrict_to_categories'] != "")
			$smarty->assign('COUPON_RESTRICT_TO_CATEGORIES', $coupon_result->fields['restrict_to_categories']);	

		if ($coupon_result->fields['coupon_type'] != 'G') {
			$smarty->assign('COUPON_START_DATE', date_short($coupon_result->fields['coupon_start_date']));
			$smarty->assign('COUPON_EXPIRE_DATE', date_short($coupon_result->fields['coupon_expire_date']));
		}
		
		if ($coupon_result->fields['coupon_type'] == 'G') {
			$html_mail = $smarty->fetch('html:send_gift');
			$html_mail .= $signatur_html;
			$txt_mail = $smarty->fetch('txt:send_gift');
			$txt_mail .= $signatur_text;
			$mail_data = get_mail_data('send_gift');
		
		} else {
			$html_mail = $smarty->fetch('html:send_coupon');
			$html_mail .= $signatur_html;
			$txt_mail = $smarty->fetch('txt:send_coupon');
			$txt_mail .= $signatur_text;
			$mail_data = get_mail_data('send_coupon');
		}

		php_mail($mail_data['EMAIL_ADDRESS'],
				$mail_data['EMAIL_ADDRESS_NAME'],
				$mail->fields['customers_email_address'], 
				$mail->fields['customers_firstname'].' '.$mail->fields['customers_lastname'] , 
				'', 
				$mail_data['EMAIL_REPLAY_ADDRESS'], 
				$mail_data['EMAIL_REPLAY_ADDRESS_NAME'],
				'', 
				'', 
				empty($subject) ? $mail_data['EMAIL_SUBJECT'] : $subject, 
				$html_mail, 
				$txt_mail);

		$db->db_query("INSERT INTO 
							".TABLE_COUPON_EMAIL_TRACK." 
							(coupon_id, 
							customer_id_sent, 
							sent_firstname, 
							sent_lastname, 
							emailed_to, 
							date_sent) 
						VALUES 
							('".(int)$_GET['cid'] ."', 
							'".$mail->fields['customers_id']."', 
							'".$mail->fields['customers_firstname']."', 
							'".$mail->fields['customers_lastname']."', 
							'".$mail->fields['customers_email_address']."', 
							NOW())");
		
		$mail->MoveNext();
	}
    redirect(href_link(FILENAME_COUPON_ADMIN, 'mail_sent_to='.urlencode($mail_sent_to)));
}

if (($_GET['action'] == 'preview_email') && (!$_POST['customers_email_address'])) {
	$_GET['action'] = 'email';
	$message_stack->add(ERROR_NO_CUSTOMER_SELECTED, 'error');
}
if ($_GET['mail_sent_to'])
	$message_stack->add(sprintf(NOTICE_EMAIL_SENT_TO, $_GET['mail_sent_to']), 'notice');

switch ($_GET['action']) {
	case 'confirmdelete':
		$del_cid = (int)$_GET['cid'];

        $db->db_query("DELETE FROM ".TABLE_COUPONS." WHERE coupon_id = ".$db->db_prepare($del_cid));
        $db->db_query("DELETE FROM ".TABLE_COUPONS_DESCRIPTION." WHERE coupon_id = ".$db->db_prepare($del_cid));
		
		redirect(href_link(FILENAME_COUPON_ADMIN, 'page='.(int)$_GET['page']));	
		break;

	case 'confirmdeleteinactive':
        $inactive = $db->db_query("SELECT coupon_id FROM ".TABLE_COUPONS." WHERE coupon_active = 'N'");
		 while(!$inactive->EOF) {
	        $db->db_query("DELETE FROM ".TABLE_COUPONS." WHERE coupon_id = '".$inactive['coupon_id']."'");
    	    $db->db_query("DELETE FROM ".TABLE_COUPONS_DESCRIPTION." WHERE coupon_id = '".$inactive['coupon_id']."'");
		}
		redirect(href_link(FILENAME_COUPON_ADMIN, ''));			
		break;

	case 'update':
		$languages = get_languages();
		$_POST['coupon_code'] = trim($_POST['coupon_code']);
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
        	$language_id = $languages[$i]['id'];
        	$_POST['coupon_name'][$language_id] = trim($_POST['coupon_name'][$language_id]);
        	$_POST['coupon_desc_'.$language_id] = trim($_POST['coupon_desc_'.$language_id]);
        }
		$_POST['coupon_amount'] = trim($_POST['coupon_amount']);
		$update_errors = 0;
		if (!$_POST['coupon_amount'] && $_POST['coupon_type'] != "S") {
			$update_errors = 1;
			$message_stack->add(ERROR_NO_COUPON_AMOUNT, 'error');
		}
		if (!$_POST['coupon_type']) {
			$update_errors = 1;
			$message_stack->add(ERROR_NO_COUPON_TYPE, 'error');
		}
		if (!$_POST['coupon_code'])
			$coupon_code = create_coupon_code(); 
		else if ($_POST['coupon_code']) {
			$coupon_code = $_POST['coupon_code'];

	    if ($_POST['coupon_products'] != '' && $_POST['coupon_categories'] != '')
			$update_errors = 1;
			$message_stack->add(ERROR_DOUBLE_PRODUCTS_CATS, 'error');
		}
		
		$check = $db->db_query("SELECT coupon_code FROM ".TABLE_COUPONS." WHERE coupon_code = ".$db->db_prepare($coupon_code));
		
		if ($check->_numOfRows && $_POST['coupon_code'] && $_GET['oldaction'] != 'voucheredit')  {
        	$update_errors = 1;
	        $message_stack->add(ERROR_COUPON_EXISTS, 'error');
      	}
      	if ($update_errors != 0)
        	$_GET['action'] = 'new';
      	else 
        	$_GET['action'] = 'update_preview';
	break;
  
    case 'update_confirm':
	    if (($_POST['back_x']) || ($_POST['back_y']))
    		$_GET['action'] = "new";
      	else  {
	        $sql_data_array = array('coupon_code' => $_POST['coupon_code'], 
	        						'coupon_amount' => $_POST['coupon_amount'], 
	        						'coupon_type' => $_POST['coupon_type'], 
	        						'uses_per_coupon' => $_POST['coupon_uses_coupon'], 
	        						'uses_per_user' => $_POST['coupon_uses_user'], 
	        						'coupon_minimum_order' => $_POST['coupon_min_order'], 
	        						'restrict_to_products' => $_POST['coupon_products'], 
	        						'restrict_to_categories' => $_POST['coupon_categories'], 
	        						'coupon_start_date' => $_POST['coupon_startdate'], 
	        						'coupon_expire_date' => $_POST['coupon_finishdate'], 
	        						'date_created' => 'NOW()', 
	        						'date_modified' => 'NOW()');
	        						
			$languages = get_languages();
			for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
				$language_id = $languages[$i]['id'];
				$sql_data_marray[$i] = array('coupon_name' => $_POST['coupon_name'][$language_id], 'coupon_description' => $_POST['coupon_desc_'.$language_id]);
        	}
        	if ($_GET['oldaction'] == "voucheredit") {
				$db->db_perform(TABLE_COUPONS, $sql_data_array, 'update', "coupon_id='".(int)$_GET['cid']."'");
    			for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
	            	$language_id = (int)$languages[$i]['id'];
            		$update = $db->db_query("UPDATE 
            									".TABLE_COUPONS_DESCRIPTION." 
            								SET 
            									coupon_name = '".$_POST['coupon_name'][$language_id]."', 
            									coupon_description = '".$_POST['coupon_desc_'.$language_id]."' 
            								WHERE 
            									coupon_id = '".(int)$_GET['cid']."' 
            								AND 
            									language_id = '".$language_id."'");
          		}
        	} else {
          		$query = $db->db_perform(TABLE_COUPONS, $sql_data_array);
          		$insert_id = $db->db_insert_id($query);
		        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
            		$language_id = $languages[$i]['id'];
            		$sql_data_marray[$i]['coupon_id'] = $insert_id;
            		$sql_data_marray[$i]['language_id'] = $language_id;
            		$db->db_perform(TABLE_COUPONS_DESCRIPTION, $sql_data_marray[$i]);
          		}
      		}
    	}
	break;

}
require(DIR_WS_INCLUDES.'metatag.php'); ?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellspacing="0" cellpadding="0">
	<tr>
    	<td width="<?php echo BOX_WIDTH; ?>" valign="top" class="columnLeft2">
			<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
		</td>
		<?php 
		switch ($_GET['action']) {
			case 'voucherreport':
		?>
		<td class="boxCenter" valign="top">
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="0">
							<tr>
								<td class="pageHeading">
									<?php echo HEADING_TITLE; ?>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
				<td>
					<table border="0" width="100%" cellspacing="0" cellpadding="0">
          				<tr>
		            		<td valign="top">
								<table border="0" width="100%" cellspacing="0" cellpadding="2">
              						<tr class="dataTableHeadingRow">
                						<td class="dataTableHeadingContent">
											<?php echo CUSTOMER_ID; ?>
										</td>	
        		        				<td class="dataTableHeadingContent" align="center">
											<?php echo CUSTOMER_NAME; ?>
										</td>	
                						<td class="dataTableHeadingContent" align="center">
											<?php echo IP_ADDRESS; ?>
										</td>	
                						<td class="dataTableHeadingContent" align="center">
											<?php echo REDEEM_DATE; ?>
										</td>	
              						</tr>
									<?php 
									$sql = "SELECT * FROM ".TABLE_COUPON_REDEEM_TRACK." WHERE coupon_id = '".(int)$_GET['cid']."'";
                                	$query = page_break_create($_POST['per_site'], $_GET['page'], $sql, 'coupon_admin.php');
                                	$cc_list = $query['query'];
								    while (!$cc_list->EOF) {
								    	$rows++;
								    	if (strlen($rows) < 2) {
										    $rows = '0'.$rows;
     									}
									    if (((!$_GET['uid']) || (@$_GET['uid'] == $cc_list->fields['unique_id'])) && (!$cInfo)) {
       										$cInfo = new objectInfo($cc_list->fields);
	     								}
								    	if ((is_object($cInfo)) && ($cc_list->fields['unique_id'] == $cInfo->unique_id)) {
       										echo '<tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'pointer\'" onclick="document.location.href=\''.href_link('coupon_admin.php', get_all_get_params(array('cid', 'action', 'uid')).'cid='.$cInfo->coupon_id.'&action=voucherreport&uid='.$cinfo->unique_id).'\'">'."\n";
		   								} else {
       										echo '<tr class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\'dataTableRow\'" onclick="document.location.href=\''.href_link('coupon_admin.php', get_all_get_params(array('cid', 'action', 'uid')).'cid='.$cc_list->fields['coupon_id'].'&action=voucherreport&uid='.$cc_list->fields['unique_id']).'\'">'."\n";
										}
										$customer = $db->db_query("SELECT 
																		customers_firstname, 
																		customers_lastname
																	FROM 
																		".TABLE_CUSTOMERS." 
																	WHERE 
																		customers_id = '".$cc_list->fields['customer_id']."'");
									?>
				           				<td class="dataTableContent">
											<?php echo $cc_list->fields['customer_id']; ?>
										</td>
                						<td class="dataTableContent" align="center">
											<?php echo $customer->fields['customers_firstname'].' '.$customer->fields['customers_lastname']; ?>
										</td>
                						<td class="dataTableContent" align="center">
											<?php echo $cc_list->fields['redeem_ip']; ?>
										</td>
		                				<td class="dataTableContent" align="center">
											<?php echo date_short($cc_list->fields['redeem_date']); ?>
										</td>
        		      				</tr>
									<?php
										$cc_list->MoveNext(); 
										} ?>
		 	            		</table>
							</td>
							<?php
							// Box linke Seite
							$heading = array();
							$contents = array();
			    			$coupon_desc = $db->db_query("SELECT coupon_name FROM ".TABLE_COUPONS_DESCRIPTION." WHERE coupon_id = '".$_GET['cid']."' AND language_id = '".$_SESSION['languages_id']."'");

							$count_customers = $db->db_query("SELECT * FROM ".TABLE_COUPON_REDEEM_TRACK." WHERE coupon_id = '".$_GET['cid']."' AND customer_id = '".$cInfo->customer_id."'");
							$heading[] = array('text' => '<b>['.$_GET['cid'].']'.COUPON_NAME.' '.$coupon_desc->fields['coupon_name'].'</b>');
							$contents[] = array('text' => '<b>'.TEXT_REDEMPTIONS.'</b>');
							$contents[] = array('text' => TEXT_REDEMPTIONS_TOTAL.'='.$cc_list->_numOfRows);
							$contents[] = array('text' => TEXT_REDEMPTIONS_CUSTOMER.'='.$count_customers->_numOfRows);
							$contents[] = array('text' => '<center><a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', 'cid='.$_GET['cid'].'&page='.$_GET['page'], 'NONSSL').'">'.BUTTON_BACK.'</a>');													
							?>
							<td width="25%" valign="top" class="border">
								<?php
						      	$box = new box;
							    echo $box->infoBox($heading, $contents);
							?>
					      	</td>
					      	<?php
	
			break;
			
	case 'preview_email': 
    	$coupon_result = $db->db_query("SELECT 
    										coupon_code 
    									FROM 
    										".TABLE_COUPONS." 
    									WHERE 
    										coupon_id = '".(int)$_GET['cid']."'");

	    $coupon_name = $db->db_query("SELECT 
	    									coupon_name 
	    								FROM 
	    									".TABLE_COUPONS_DESCRIPTION." 
	    								WHERE 
	    									coupon_id = '".(int)$_GET['cid']."' 
	    								AND 
	    									language_id = '".(int)$_SESSION['languages_id']."'");

		if (is_numeric($_POST['customers_email_address'])) {
			$customers_status = $db->db_query("SELECT 	
													customers_status_id, 
													customers_status_name 
												FROM 
													".TABLE_CUSTOMERS_STATUS." 
												WHERE 
													language_id = '".$_SESSION['languages_id']."' 
												AND 
													customers_status_id = '".$_POST['customers_email_address']."'");
													
			$mail_sent_to = $customers_status->fields['customers_status_name'];
			
		} else {
	    	switch ($_POST['customers_email_address']) {
    			case '***':
		      		$mail_sent_to = TEXT_ALL_CUSTOMERS;
		    	break;
		    	case '**D':
		      		$mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
		    	break;
		    	default:
		      		$mail_sent_to = $_POST['customers_email_address'];
		    	break;
	    	}
		}
		?>
      	<td class="boxCenter" valign="top">
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
     			<tr>
        			<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="0">
          					<tr>
            					<td class="pageHeading">
									<?php echo HEADING_EMAIL_TITLE; ?>
								</td>
          					</tr>
        				</table>
					</td>
      			</tr>
       			<tr>
					<td>
		            <?php echo draw_form('mail', FILENAME_COUPON_ADMIN, 'action=send_email_to_user&cid='.(int)$_GET['cid']); ?>
						<table border="0" width="100%" cellpadding="0" cellspacing="2">
              				<tr>
                				<td class="smallText">
									<b><?php echo TEXT_CUSTOMER; ?></b><br /><?php echo $mail_sent_to; ?>
								</td>
              				</tr>
              				<tr>
                				<td class="smallText">
									<b><?php echo TEXT_COUPON; ?></b><br /><?php echo $coupon_name->fields['coupon_name']; ?>
								</td>
              				</tr>
              				<tr>
                				<td class="smallText">
									<b><?php echo TEXT_SUBJECT; ?></b><br /><?php echo htmlspecialchars(stripslashes($_POST['subject'])); ?>
								</td>
              				</tr>
              				<tr>
                				<td class="smallText">
									<b><?php echo TEXT_MESSAGE; ?></b><br /><?php echo nl2br(htmlspecialchars(stripslashes($_POST['message_gv']))); ?>
								</td>
              				</tr>
              				<tr>
                				<td>
									<?php
								    reset($_POST);
								    while (list($key, $value) = each($_POST)) {
								    	if (!is_array($_POST[$key])) {
									     echo draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
      									}
    								}
									?>
					                <table border="0" width="100%" cellpadding="0" cellspacing="2">
                  						<tr>
                    						<td>
												<?php echo '&nbsp;' ?>
											</td>
						                    <td align="right">
												<?php 
													echo '<a class="button" onclick="this.blur();" href="'.href_link(FILENAME_COUPON_ADMIN).'">'.BUTTON_CANCEL.'</a> 
													<button type="submit" class="button" onclick="this.blur();">'.BUTTON_SEND_EMAIL.'</button>'; ?>
											</td>
					                  	</tr>
				                	</table>
								</td>
			            	</tr>
						</table>
						</form>
					</td>
				</tr>
<?php 
    break;
       
	case 'email':
    	$coupon_result = $db->db_query("SELECT 
    										coupon_code 
    									FROM 
    										".TABLE_COUPONS." 
    									WHERE 
    										coupon_id = '".(int)$_GET['cid']."'");

    	$coupon_name = $db->db_query("SELECT 
    										coupon_name 
    									FROM 
    										".TABLE_COUPONS_DESCRIPTION." 
    									WHERE 
    										coupon_id = '".(int)$_GET['cid']."' 
    									AND 
    										language_id = '".(int)$_SESSION['languages_id']."'");

		?>
			<td class="boxCenter" valign="top">
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
        				<td>
							<table border="0" width="100%" cellspacing="0" cellpadding="0">
          						<tr>
            						<td class="pageHeading">
										<?php echo HEADING_EMAIL_TITLE; ?>
									</td>
          						</tr>
        					</table>
						</td>
      				</tr>
      				<tr>
      					<td>
							<?php echo draw_form('mail', FILENAME_COUPON_ADMIN, 'action=preview_email&cid='.(int)$_GET['cid']); ?>
							<table border="0" cellpadding="0" cellspacing="2">
								<?php
								$customers = array();
								$customers[] = array('id' => '', 'text' => TEXT_SELECT_CUSTOMER);
								$customers[] = array('id' => '', 'text' => '------------');
							    $customers[] = array('id' => '***', 'text' => TEXT_ALL_CUSTOMERS);
							    $customers[] = array('id' => '**D', 'text' => TEXT_NEWSLETTER_CUSTOMERS);
								$customers[] = array('id' => '', 'text' => '------------');
								$customers_status = $db->db_query("SELECT 
																		customers_status_id, 
																		customers_status_name 
																	FROM 
																		".TABLE_CUSTOMERS_STATUS." 
																	WHERE 
																		language_id = '".$_SESSION['languages_id']."' 
																	ORDER BY 
																		customers_status_id");
								while(!$customers_status->EOF) {
								    $customers[] = array('id' => $customers_status->fields['customers_status_id'], 
								    					'text' => $customers_status->fields['customers_status_name']);
								    $customers_status->Movenext();
								}	
								$customers[] = array('id' => '', 'text' => '------------');	
							    $customers_values = $db->db_query("SELECT 
									    								customers_email_address, 
									    								customers_firstname, 
									    								customers_lastname 
									    							FROM 
									    								".TABLE_CUSTOMERS." 
									    							ORDER BY 
									    								customers_lastname");
    							while(!$customers_values->EOF) {
								    $customers[] = array('id' => $customers_values->fields['customers_email_address'], 	
								    					'text' => $customers_values->fields['customers_lastname'].', '.$customers_values->fields['customers_firstname'].' ('.$customers_values->fields['customers_email_address'].')');
								    $customers_values->MoveNext();
							    }
								?>
              					<tr>
                					<td class="main">
										<?php echo '<b>'.TEXT_COUPON.'</b>'; ?>
									</td>
						    	    <td class="main">
										<?php echo $coupon_name->fields['coupon_name']; ?>
									</td>
              					</tr>
              					<tr>
                					<td class="main">
										<?php echo '<b>'.TEXT_CUSTOMER.'</b>'; ?>
									</td>
                					<td class="main">
										<?php echo draw_pull_down_menu('customers_email_address', $customers, $_GET['customer']);?>
									</td>
              					</tr>
              					<tr>
                					<td class="main">
										<?php echo '<b>'.TEXT_SUBJECT.'</b>'; ?>
									</td>
                					<td class="main">
										<?php echo draw_input_field('subject'); ?>
									</td>
              					</tr>
              					<tr>
                					<td valign="top" class="main">
										<?php echo '<b>'.TEXT_MESSAGE.'</b>'; ?>
									</td>
                					<td class="main">
										<?php echo wysiwyg('message_gv', $_POST['message_gv']); ?>
									</td>
              					</tr>
              					<tr>
                					<td colspan="2" align="right">
										<?php echo '<button type="submit" class="button" onclick="this.blur();">'.BUTTON_SEND_EMAIL.'</button>'; ?>
									</td>
              					</tr>
            				</table>
            				</form>
						</td>
					</tr>
			    </table>
			</td>
<?php      
    break;

	case 'update_preview':
?>
    	<td class="boxCenter" valign="top">
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
      			<tr>
        			<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="0">
          					<tr>
            					<td class="pageHeading">
									<?php echo TEXT_HEADING_NEW_COUPON; ?>
								</td>
          					</tr>
        				</table>
					</td>
      			</tr>
      			<tr>
      				<td>
						<?php if ($_POST['coupon_code'])
							$c_code = $_POST['coupon_code'];
					    else
						    $c_code = $coupon_code;
					    ?>
						<?php echo draw_form('coupon', 'coupon_admin.php', 'action=update_confirm&oldaction='.$_GET['oldaction'].'&cid='.(int)$_GET['cid']); ?>
					    <table border="0" align="center"  width="70%" cellspacing="0" cellpadding="6">
							<?php
					        $languages = get_languages();
					        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
					            $language_id = $languages[$i]['id'];
							?>
						    <tr>
						        <td align="left" class="main" width="180px">
									<b><?php echo COUPON_NAME.' '.image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/'.$languages[$i]['image']); ?> </b>
								</td>
    						    <td align="left" class="main" >
									<?php 
									if (!$_POST['coupon_name'][$language_id])
										$_POST['coupon_name'][$language_id] = $coupon_code; 
			
									echo $_POST['coupon_name'][$language_id]; 
									?>
								</td>
						    </tr>
							<?php
							}
					        $languages = get_languages();
						    for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
					            $language_id = $languages[$i]['id'];
							?>
					      	<tr>
						        <td align="left" class="main" >
									<b><?php echo COUPON_DESC.' '.image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/admin/'.$languages[$i]['image']); ?></b>
								</td>
						        <td align="left" class="main" >
									<?php echo $_POST['coupon_desc'][$language_id]; ?>
								</td>
						    </tr>
							<?php }	?>
							<tr>
						        <td align="left" class="main">
									<b><?php echo COUPON_TYPE; ?></b>
								</td>
								<?php if ($_POST['coupon_type'] == "S") { ?>
        						<td align="left" class="main">
									<?php echo TEXT_FREE_SHIPPING; ?>
								</td>
								<?php } else if ($_POST['coupon_type'] == "F") { ?>
        						<td align="left" class="main">
									<?php echo TYPE_F; ?>
								</td>
								<?php } else if ($_POST['coupon_type'] == "P") { ?>
        						<td align="left" class="main">
									<?php echo TYPE_P; ?>
								</td>				
								<?php } else if ($_POST['coupon_type'] == "G") { ?>
        						<td align="left" class="main">
									<?php echo TYPE_G; ?>
								</td>								
								<?php } ?>
      						</tr>						
							<?php if ($_POST['coupon_type'] != "S") { ?>
						    <tr>
						        <td align="left" class="main" >
									<b><?php echo COUPON_AMOUNT; ?></b>
								</td>
						        <td align="left" class="main" >
									<?php 
									if ($_POST['coupon_type'] != "P") { 
										echo $currencies->format($_POST['coupon_amount']); 
									} else {
										echo $_POST['coupon_amount'].' %';
									}
									?>
								</td>
						    </tr>
							<?php }	?>							
	  						<tr>
        						<td align="left" class="main" >
									<b><?php echo COUPON_CODE; ?></b>
								</td>
						        <td align="left" class="main" class="main">
									<?php echo $coupon_code; ?>
								</td>
							</tr>
							<?php if ($_POST['coupon_type'] != "G") { ?>
							<tr>
						        <td align="left" class="main">
									<b><?php echo COUPON_MIN_ORDER; ?></b>
								</td>
        						<td align="left" class="main">
									<?php echo $_POST['coupon_min_order']; ?>
								</td>
      						</tr>
							<tr>
						        <td align="left" class="main">
									<b><?php echo COUPON_USES_COUPON; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo $_POST['coupon_uses_coupon']; ?>
								</td>
						    </tr>
						    <tr>
						        <td align="left" class="main">
									<b><?php echo COUPON_USES_USER; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo $_POST['coupon_uses_user']; ?>
								</td>
						    </tr>
       						<tr>
        						<td align="left" class="main">
									<b><?php echo COUPON_PRODUCTS; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo $_POST['coupon_products']; ?>
								</td>
						    </tr>
						    <tr>
						        <td align="left" class="main">
									<b><?php echo COUPON_CATEGORIES; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo $_POST['coupon_categories']; ?>
								</td>
						    </tr>
						    <tr>
						        <td align="left" class="main" >
									<b><?php echo COUPON_STARTDATE; ?></b>
								</td>
								<?php $start_date = date(DATE_FORMAT, mktime(0, 0, 0, $_POST['coupon_startdate_month'],$_POST['coupon_startdate_day'] ,$_POST['coupon_startdate_year'] )); ?>
						        <td align="left" class="main" >
									<?php echo $start_date; ?>
								</td>
						    </tr>
							<tr>
						        <td align="left" class="main" ><b><?php echo COUPON_FINISHDATE; ?></b></td>
								<?php
							    $finish_date = date(DATE_FORMAT, mktime(0, 0, 0, $_POST['coupon_finishdate_month'],$_POST['coupon_finishdate_day'] ,$_POST['coupon_finishdate_year'] ));
								?>
						        <td align="left" class="main" ><?php echo $finish_date; ?></td>
						    </tr>
							<?php }	?>													
							<?php
					        $languages = get_languages();
					        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
						        $language_id = $languages[$i]['id'];
						        echo draw_hidden_field('coupon_name['.$languages[$i]['id'].']', $_POST['coupon_name'][$language_id]);
						        echo draw_hidden_field('coupon_desc['.$languages[$i]['id'].']', $_POST['coupon_desc'][$language_id]);
						     }
							 echo draw_hidden_field('coupon_type', $_POST['coupon_type']);
							 echo draw_hidden_field('coupon_amount', $_POST['coupon_amount']);
							 echo draw_hidden_field('coupon_code', $c_code);
							 echo draw_hidden_field('coupon_min_order', $_POST['coupon_min_order']);
							 echo draw_hidden_field('coupon_uses_coupon', $_POST['coupon_uses_coupon']);
							 echo draw_hidden_field('coupon_uses_user', $_POST['coupon_uses_user']);
							 echo draw_hidden_field('coupon_products', $_POST['coupon_products']);
							 echo draw_hidden_field('coupon_categories', $_POST['coupon_categories']);
							 echo draw_hidden_field('coupon_startdate', date('Y-m-d', mktime(0, 0, 0, $_POST['coupon_startdate_month'],$_POST['coupon_startdate_day'] ,$_POST['coupon_startdate_year'] )));
							 echo draw_hidden_field('coupon_finishdate', date('Y-m-d', mktime(0, 0, 0, $_POST['coupon_finishdate_month'],$_POST['coupon_finishdate_day'] ,$_POST['coupon_finishdate_year'] )));
						     ?>
						     <tr>
						        <td align="left">
									<?php echo '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', 'action='.$_GET['oldaction'].'&cid='.$_GET['cid'].'&page='.$_GET['page'], 'NONSSL').'">'.BUTTON_BACK.'</a>'; ?></a>
								</td>
        						<td align="left">
									<?php echo '<button type="submit" class="button" onclick="this.blur();">'.BUTTON_CONFIRM.'</button>'; ?>
								</td>
							</tr>
						</table>
						</form>
      				</td>
				</tr>
		    </table>
		</td>
<?php      
	break;

	case 'voucheredit':
    	$languages = get_languages();
    	for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
			$language_id = $languages[$i]['id'];
		    $coupon = $db->db_query("SELECT 
		    							coupon_name,
		    							coupon_description 
		    						FROM 
		    							".TABLE_COUPONS_DESCRIPTION." 
		    						WHERE 
		    							coupon_id = '".(int)$_GET['cid']."' 
		    						AND 
		    							language_id = '".$language_id."'");

      		$coupon_name[$language_id] = $coupon->fields['coupon_name'];
      		$coupon_desc[$language_id] = $coupon->fields['coupon_description'];
    	}
    	$coupon = $db->db_query("SELECT 
    								coupon_code, 
    								coupon_amount, 
    								coupon_type, 
    								coupon_minimum_order, 
    								coupon_start_date, 
    								coupon_expire_date,
    								uses_per_coupon, 
    								uses_per_user, 
    								restrict_to_products, 
    								restrict_to_categories 
    							FROM 
    								".TABLE_COUPONS." 
    							WHERE 
    								coupon_id = '".(int)$_GET['cid']."'");

	    $coupon_code = $coupon->fields['coupon_code'];
		$coupon_amount = $coupon->fields['coupon_amount'];
    	$coupon_type = $coupon->fields['coupon_type'];
    	$coupon_min_order = $coupon->fields['coupon_minimum_order'];
		$coupon_startdate = explode("-", $coupon->fields['coupon_start_date']);
		$coupon_finishdate = explode("-", $coupon->fields['coupon_expire_date']);
	    $coupon_uses_coupon = $coupon->fields['uses_per_coupon'];
    	$coupon_uses_user = $coupon->fields['uses_per_user'];
	    $coupon_products = $coupon->fields['restrict_to_products'];
    	$coupon_categories = $coupon->fields['restrict_to_categories'];     

		
	case 'new':
		if ($_GET['action'] != "voucheredit") {
			$languages = get_languages();
    		for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
				$language_id = $languages[$i]['id'];
      			$coupon_name[$language_id] = $_POST['coupon_name'][$language_id];
	      		$coupon_desc[$language_id] = $_POST['coupon_desc'][$language_id];
    		}
	    	$coupon_amount = $_POST['coupon_amount'];
	    	$coupon_type = $_POST['coupon_type'];
    		$coupon_min_order = $_POST['coupon_min_order'];
	    	$coupon_code = $_POST['coupon_code'];
		    $coupon_uses_coupon = $_POST['coupon_uses_coupon'];
    		$coupon_uses_user = $_POST['coupon_uses_user'];
	    	$coupon_products = $_POST['coupon_products'];
	    	$coupon_categories = $_POST['coupon_categories'];     
			
	    	if (!$_POST['coupon_startdate_year'])
			    $coupon_startdate = explode("-", date('Y-m-d'));
		    else {
			    $coupon_startdate = array();
		    	$coupon_startdate[0] = $_POST['coupon_startdate_year'];
			    $coupon_startdate[2] = $_POST['coupon_startdate_day'];
			    $coupon_startdate[1] = $_POST['coupon_startdate_month'];
	    	}
		    if (!$_POST['coupon_finishdate_year']) {
			    $coupon_finishdate = explode("-", date('Y-m-d'));
			    $coupon_finishdate[0] = $coupon_finishdate[0] + 1;
		    } else {
			    $coupon_finishdate = array();
			    $coupon_finishdate[0] = $_POST['coupon_finishdate_year'];
		    	$coupon_finishdate[2] = $_POST['coupon_finishdate_day'];
			    $coupon_finishdate[1] = $_POST['coupon_finishdate_month'];
		    }		
		}	
		?>
		<script type="text/javascript">
			function SetVisHide() {
				document.getElementById('coupon1').className = "dn";
				document.getElementById('coupon2').className = "dn";		
				document.getElementById('coupon3').className = "dn";
				document.getElementById('coupon4').className = "dn";
				document.getElementById('coupon5').className = "dn";
				document.getElementById('coupon6').className = "dn";
				document.getElementById('coupon7').className = "dn";
			}
			function SetVisShow() {
				document.getElementById('coupon1').className = "";
				document.getElementById('coupon2').className = "";		
				document.getElementById('coupon3').className = "";		
				document.getElementById('coupon4').className = "";
				document.getElementById('coupon5').className = "";
				document.getElementById('coupon6').className = "";		
				document.getElementById('coupon7').className = "";
			}
			function SetVisHideAmount() {
				document.getElementById('amount').className = "dn";
			}
			function SetVisShowAmount() {
				document.getElementById('amount').className = "";
			}

		</script>  
	    <td  class="boxCenter" valign="top">
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
			    <tr>
        			<td>
						<table border="0" width="100%" cellspacing="0" cellpadding="0">
          					<tr>
            					<td class="pageHeading">
									<?php echo TEXT_HEADING_NEW_COUPON; ?>
								</td>
          					</tr>
        				</table>
					</td>
      			</tr>
      			<tr>
      				<td>
						<?php 
						if ($_GET['oldaction'] != '')
							echo draw_form('coupon', 'coupon_admin.php', 'action=update&oldaction='.$_GET['oldaction'].'&cid='.(int)$_GET['cid'], 'post', 'enctype="multipart/form-data"'); 
						else if ($_GET['action'] != '') 
							echo draw_form('coupon', 'coupon_admin.php', 'action=update&oldaction='.$_GET['action'].'&cid='.(int)$_GET['cid'], 'post', 'enctype="multipart/form-data"'); 						
						?>
					    <table border="0" cellspacing="0" cellpadding="6">
							<?php 
							$languages = get_languages();
						    for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
							    $language_id = $languages[$i]['id'];
							?>
      						<tr>
        						<td align="left" class="main" width="200px">
									<b><?php echo COUPON_NAME.' '.image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/'.$languages[$i]['image']) ?></b>
								</td>
						        <td align="left" width="220px">
									<?php echo draw_input_field('coupon_name['.$languages[$i]['id'].']', $coupon_name[$language_id]); ?>
								</td>
						        <td align="left" class="main">
									<?php if ($i==0) echo COUPON_NAME_HELP; ?>
								</td>
						    </tr>
							<?php 
							}
					        $languages = get_languages();
					        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
						        $language_id = $languages[$i]['id'];
							?>
						    <tr>
        						<td align="left" valign="top" class="main">
									<b><?php echo COUPON_DESC.' '.image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/'.$languages[$i]['image']) ?></b>
								</td>
        						<td align="left" valign="top" colspan="2">
									<?php echo wysiwyg('coupon_desc_'.$languages[$i]['id'], $coupon_desc[$language_id])?>
								</td>
							</tr>
							<?php } ?>
							<tr>
								<td align="left" class="main">
									<b><?php echo COUPON_TYPE; ?></b>
								</td>
						        <td align="left" class="main">
									<?php 
									if ($coupon_type == "G")
										echo '<input type="radio" name="coupon_type" value="G" checked="checked" onclick="SetVisHide(); SetVisShowAmount();" />'.TYPE_G; 
									else
										echo '<input type="radio" name="coupon_type" value="G" onclick="SetVisHide(); SetVisShowAmount();" />'.TYPE_G; 
									?>
								</td>
						        <td align="left" class="main">
									<?php echo TYPE_G_HELP; ?>
								</td>
						    </tr>
							<tr>
								<td align="left" class="main">
								</td>
						        <td align="left" class="main">
									<?php 
									if ($coupon_type == "F")
										echo '<input type="radio" name="coupon_type" value="F" checked="checked" onclick="SetVisShow(); SetVisShowAmount();" />'.TYPE_F;
									else
										echo '<input type="radio" name="coupon_type" value="F" onclick="SetVisShow(); SetVisShowAmount();" />'.TYPE_F;
									?>								
								</td>
						        <td align="left" class="main">
									<?php echo TYPE_F_HELP; ?>
								</td>
						    </tr>
							<tr>
								<td align="left" class="main">
								</td>
						        <td align="left" class="main">
									<?php 
									if ($coupon_type == "P")
										echo '<input type="radio" name="coupon_type" value="P" checked="checked" onclick="SetVisShow(); SetVisShowAmount();" />'.TYPE_P;
									else
										echo '<input type="radio" name="coupon_type" value="P" onclick="SetVisShow(); SetVisShowAmount();" />'.TYPE_P;
									?>								
								</td>
						        <td align="left" class="main">
									<?php echo TYPE_P_HELP; ?>
								</td>
						    </tr> 
							<tr>
								<td align="left" class="main">
								</td>
						        <td align="left" class="main">
									<?php 
									if ($coupon_type == "S")
										echo '<input type="radio" name="coupon_type" value="S" checked="checked" onclick="SetVisShow(); SetVisHideAmount();" />'.TYPE_S;
									else
										echo '<input type="radio" name="coupon_type" value="S" onclick="SetVisShow(); SetVisHideAmount();" />'.TYPE_S;
									?>																
								</td>
						        <td align="left" class="main">
									<?php echo TYPE_S_HELP; ?>
								</td>
						    </tr> 
							<tr id="amount" class="show">
							    <td align="left" class="main">
									<b><?php echo COUPON_AMOUNT; ?></b>
								</td>
        						<td align="left" class="main">
									<?php echo draw_input_field('coupon_amount', $coupon_amount); ?>
								</td>
        						<td align="left" class="main">
									<?php echo COUPON_AMOUNT_HELP; ?>
								</td>
      						</tr>
      						<tr>
        						<td align="left" class="main">
									<b><?php echo COUPON_CODE; ?></b>
								</td>
        						<td align="left" class="main">
									<?php echo draw_input_field('coupon_code', $coupon_code); ?>
								</td>
        						<td align="left" class="main">
									<?php echo COUPON_CODE_HELP; ?>
								</td>
      						</tr>
      						<tr id="coupon1" class="show">
        						<td align="left" class="main">
									<b><?php echo COUPON_MIN_ORDER; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo draw_input_field('coupon_min_order', $coupon_min_order); ?>
								</td>
						        <td align="left" class="main">
									<?php echo COUPON_MIN_ORDER_HELP; ?>
								</td>
						    </tr>
						    <tr id="coupon2" class="show">
						        <td align="left" class="main">
									<b><?php echo COUPON_USES_COUPON; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo draw_input_field('coupon_uses_coupon', $coupon_uses_coupon); ?>
								</td>
						        <td align="left" class="main">
									<?php echo COUPON_USES_COUPON_HELP; ?>
								</td>
						    </tr>
      						<tr id="coupon3" class="show">
        						<td align="left" class="main">
									<b><?php echo COUPON_USES_USER; ?></b>
								</td>
        						<td align="left" class="main">
									<?php echo draw_input_field('coupon_uses_user', $coupon_uses_user); ?>
								</td>
        						<td align="left" class="main">
									<?php echo COUPON_USES_USER_HELP; ?>
								</td>
      						</tr>
       						<tr id="coupon4" class="show">
						        <td align="left" class="main">
									<b><?php echo COUPON_PRODUCTS; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo draw_input_field('coupon_products', $coupon_products); ?> <a href="validproducts.php" target="_blank" onclick="window.open('validproducts.php', 'Valid_Products', 'scrollbars=yes, resizable=yes, menubar=yes, width=600, height=600'); return false;"><?php echo VIEW; ?></a></td>
						        <td align="left" class="main">
									<?php echo COUPON_PRODUCTS_HELP; ?>
								</td>
						    </tr>
						    <tr id="coupon5" class="show">
							    <td align="left" class="main">
									<b><?php echo COUPON_CATEGORIES; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo draw_input_field('coupon_categories', $coupon_categories); ?><a href="validcategories.php" target="_blank" onclick="window.open('validcategories.php', 'Valid_Categories', 'scrollbars=yes, resizable=yes, menubar=yes, width=600, height=600'); return false"><?php echo VIEW; ?></a>
								</td>
						        <td align="left" class="main">
									<?php echo COUPON_CATEGORIES_HELP; ?>
								</td>
						    </tr>
      						<tr id="coupon6" class="show">
						        <td align="left" class="main">
									<b><?php echo COUPON_STARTDATE; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo draw_date_selector('coupon_startdate', mktime(0,0,0, $coupon_startdate[1], $coupon_startdate[2], $coupon_startdate[0])); ?>
								</td>
						        <td align="left" class="main">
									<?php echo COUPON_STARTDATE_HELP; ?>
								</td>
						    </tr>
						    <tr id="coupon7" class="show">
						        <td align="left" class="main">
									<b><?php echo COUPON_FINISHDATE; ?></b>
								</td>
						        <td align="left" class="main">
									<?php echo draw_date_selector('coupon_finishdate', mktime(0,0,0, $coupon_finishdate[1], $coupon_finishdate[2], $coupon_finishdate[0])); ?>
								</td>
						        <td align="left" class="main">
									<?php echo COUPON_FINISHDATE_HELP; ?>
								</td>
						    </tr>
      						<tr>
						        <td align="left">
									<?php echo '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', ''); ?>"><?php echo BUTTON_CANCEL; ?></a>
								</td>
						        <td align="left">
									<?php echo '<input type="submit" class="button" onclick="this.blur();" value="'.BUTTON_PREVIEW.'"/>'; ?>
								</td>
						    </tr>
						</table>
						</form>
					</td>	
				</tr>
			</table>
		</td>
<?php
    break;

	default:
?>    
	<td  class="boxCenter" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
		<tr>
			<td width="100%">
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
          			<tr>
            			<td class="pageHeading">
							<?php echo HEADING_TITLE; ?>
						</td>
            			<td class="main">
							<?php echo draw_form('status', FILENAME_COUPON_ADMIN, '', 'get'); 
						    $status_array[] = array('id' => 'Y', 'text' => TEXT_COUPON_ACTIVE);
						    $status_array[] = array('id' => 'N', 'text' => TEXT_COUPON_INACTIVE);
						    $status_array[] = array('id' => '*', 'text' => TEXT_COUPON_ALL);
							if ($_GET['status'])
							    $status = $_GET['status'];
						    else
							    $status = 'Y';
						    echo TEXT_STATUS.' '.draw_pull_down_menu('status', $status_array, $status, 'onchange="this.form.submit();"');
							if ($_GET['type'])
								echo draw_hidden_field('type', $_GET['type']); 
							if ($_GET['search'])
								echo draw_hidden_field('search', $_GET['search']); 
							if ($_GET['page'])
								echo draw_hidden_field('page', $_GET['page']); 
							if ($_GET['paging'])
								echo draw_hidden_field('paging', $_GET['paging']); 
							if ($_GET['sorting'])
								echo draw_hidden_field('sorting', $_GET['sorting']); 	 
							?>
              				</form>
           				</td>
            			<td class="main">
							<?php echo draw_form('type', FILENAME_COUPON_ADMIN, '', 'get'); 
    						$type_array[] = array('id' => 'g', 'text' => 'Gutscheine');
    						$type_array[] = array('id' => 'c', 'text' => 'Kupons');
    						$type_array[] = array('id' => '*', 'text' => 'Alle');
    						if ($_GET['type'])
      							$type = $_GET['type'];
    						else
      							$type = '*';
   							echo TEXT_TYPE.' '.draw_pull_down_menu('type', $type_array, $type, 'onchange="this.form.submit();"');
							if ($_GET['status'])
								echo draw_hidden_field('status', $_GET['status']); 
							if ($_GET['search'])
								echo draw_hidden_field('search', $_GET['search']); 
							if ($_GET['page'])
								echo draw_hidden_field('page', $_GET['page']); 
							if ($_GET['paging'])
								echo draw_hidden_field('paging', $_GET['paging']); 
							if ($_GET['sorting'])
								echo draw_hidden_field('sorting', $_GET['sorting']); 	 
							?>
			            	</form>
			        	</td>	
						<td class="main">
							
						</td>
		   				<td class="main" align="right">
							<?php 
							// product search field
							echo draw_form('search', FILENAME_COUPON_ADMIN, '', 'get'); 
							echo TEXT_SEARCH.' '.draw_input_field('search', $_GET['search']);
							if($_GET['type'])
								echo draw_hidden_field('type', $_GET['type']); 
							if ($_GET['status'])
								echo draw_hidden_field('status', $_GET['status']); 
							if ($_GET['page'])
								echo draw_hidden_field('page', $_GET['page']); 
							if ($_GET['paging'])
								echo draw_hidden_field('paging', $_GET['paging']); 
							if ($_GET['sorting'])
								echo draw_hidden_field('sorting', $_GET['sorting']); 
							?>
							</form>
						</td>   	   
          			</tr>
        		</table>
			</td>
      	</tr>
      	<tr>
        	<td>
				<?php echo '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', 'action=new').'">'.BUTTON_INSERT.'</a>'; ?>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
          			<tr>
            			<td valign="top">
							<table border="0" width="100%" cellspacing="0" cellpadding="2">
					            <tr class="dataTableHeadingRow">
					                <td class="dataTableHeadingContent">
										<?php echo COUPON_NAME.sorting(FILENAME_COUPON_ADMIN,'name'); ?>
									</td>
					                <td class="dataTableHeadingContent" align="center">
										<?php echo COUPON_AMOUNT.sorting(FILENAME_COUPON_ADMIN,'coupon_amount'); ?>
									</td>	
					                <td class="dataTableHeadingContent" align="center">
										<?php echo COUPON_CODE.sorting(FILENAME_COUPON_ADMIN,'code'); ?>
									</td>	
									<td class="dataTableHeadingContent" align="center">
										<?php echo DATE_CREATED.sorting(FILENAME_COUPON_ADMIN,'date-created'); ?>
									</td>	
									<td class="dataTableHeadingContent" align="center">
										<?php echo STATUS.sorting(FILENAME_COUPON_ADMIN,'status'); ?>
									</td>					
					                <td class="dataTableHeadingContent" align="right">
										<?php echo TABLE_HEADING_ACTION; ?>
									</td>
					            </tr>
								<?php
								// GET STATUS
								if ($status == "Y")
									$where_status = " AND c.coupon_active = 'Y'";
								else if ($status == "N")
									$where_status = " AND c.coupon_active = 'N'";
								else if ($status == "*")
									$where_status = " AND (c.coupon_active = 'Y' or c.coupon_active = 'N')";
								// GET SEARCH
								if ($_GET['search'] != "")
									$where_search = " AND (cd.coupon_name like '%".$_GET['search']."%' or c.coupon_amount like '%".$_GET['search']."%' or c.coupon_code like '%".$_GET['search']."%')";
								else
									$where_search = "";
								// GET TYPE
								if ($_GET['type'] == "g")
									$where_type = " AND c.coupon_type = 'G'";
								else if ($_GET['type'] == "c")
									$where_type = " AND (c.coupon_type = 'F' or c.coupon_type = 'P' or c.coupon_type = 'S')";
								else
									$where_type = "";

								// get sorting option and switch accordingly        
								if ($_GET['sorting']) {
									switch ($_GET['sorting']){
							            case 'amount':
											$sort = ' ORDER BY c.coupon_amount ASC';            
										break;
										case 'amount-desc':
											$sort = ' ORDER BY c.coupon_amount DESC';            
										break;  
            							case 'code':
											$sort = ' ORDER BY c.coupon_code ASC';            
										break;
										case 'code-desc':
											$sort = ' ORDER BY c.coupon_code DESC';            
										break;   
							            case 'date-created':
											$sort = ' ORDER BY c.date_created ASC';            
										break;
										case 'date-created-desc':
											$sort = ' ORDER BY c.date_created DESC';            
										break;  
							            case 'status':
											$sort = ' ORDER BY c.coupon_active ASC';            
										break;
										case 'status-desc':
											$sort = ' ORDER BY c.coupon_active DESC';            
										break;  
							            case 'name':
											$sort = ' ORDER BY cd.coupon_name ASC';            
										break;
										case 'name-desc':
											$sort = ' ORDER BY cd.coupon_name DESC';            
										break;  
										default:
											$sort = ' ORDER BY cd.coupon_name ASC';    
										break;
									}
								} else
									$sort = ' ORDER BY cd.coupon_name ASC';    

								if ($_GET['paging'])
									$showpage = $_GET['paging'];
								else
									$showpage = SHOW_PAGE_STANDARD;

							    if ($_GET['page'] > 1)
									$rows = $_GET['page'] * $showpage - $showpage;

							    $sql = "SELECT 
					    						c.coupon_id, 
					    						c.coupon_code, 
					    						c.coupon_amount, 
					    						c.coupon_type, 
					    						c.coupon_start_date, 
					    						c.coupon_expire_date, 
					    						c.uses_per_user, 
					    						c.uses_per_coupon, 
					    						c.restrict_to_products, 
					    						c.restrict_to_categories, 
					    						c.date_created, 
					    						c.date_modified, 
					    						c.coupon_active, 
					    						cd.coupon_name 
					    					FROM 
					    						".TABLE_COUPONS ." c,
					    						".TABLE_COUPONS_DESCRIPTION." cd 
					    					WHERE 
					    						cd.coupon_id = c.coupon_id 
					    					AND 
					    						cd.language_id = '".$_SESSION['languages_id']."'".
					    						$where_status.$where_search.$where_type.$sort;
							    						
							    $query = page_break_create($_POST['per_site'], $_GET['page'], $sql, 'coupon_admin.php');
                                $cc_list = $query['query'];
    							while (!$cc_list->EOF) {
      								$rows++;
      								if (strlen($rows) < 2)
        								$rows = '0'.$rows;

      								if (((!$_GET['cid']) || (@$_GET['cid'] == $cc_list->fields['coupon_id'])) && (!$cInfo)) {
        								$cInfo = new objectInfo($cc_list->fields);
      								}
      								if ((is_object($cInfo)) && ($cc_list->fields['coupon_id'] == $cInfo->coupon_id))
       									echo '<tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'pointer\'" onclick="document.location.href=\''.href_link('coupon_admin.php', get_all_get_params(array('cid', 'action')).'cid='.$cInfo->coupon_id.'&action=edit').'\'">'."\n";
      								else
        								echo '<tr class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\'dataTableRow\'" onclick="document.location.href=\''.href_link('coupon_admin.php', get_all_get_params(array('cid', 'action')).'cid='.$cc_list->fields['coupon_id']).'\'">'."\n";
									?>
					                <td class="dataTableContent">
										<?php echo $cc_list->fields['coupon_name']; ?>
									</td>
                					<td class="dataTableContent" align="center">
										<?php  
      									if ($cc_list->fields['coupon_type'] == 'P')
        									echo $cc_list->fields['coupon_amount'].'%';
      									else if ($cc_list->fields['coupon_type'] == 'S')
        									echo TEXT_FREE_SHIPPING;
      									else
        									echo $currencies->format($cc_list->fields['coupon_amount']);
										?>
            						</td>
					                <td class="dataTableContent" align="center">
										<?php echo $cc_list->fields['coupon_code']; ?>
									</td>
									<td class="dataTableContent" align="center">
										<?php echo $cc_list->fields['date_created']; ?>
									</td>
									<td class="dataTableContent" align="center">
										<?php 
										if ($cc_list->fields['coupon_active'] == "Y")
											echo STATUS_ACTIVE; 
										else if ($cc_list->fields['coupon_active'] == "N")
											echo STATUS_INACTIVE; 
										?>
									</td>
									<td class="dataTableContent" align="right">
										<?php 
										if ((is_object($cInfo)) && ($cc_list->fields['coupon_id'] == $cInfo->coupon_id))
											echo image(DIR_WS_IMAGES.'icon_arrow_right.gif'); 
										else { 
											$parameters = '';
											if ($_GET['status'])
												$parameters .= '&amp;status='.$_GET['status']; 
											if ($_GET['search'])
												$parameters .= '&amp;search='.$_GET['search']; 
											if ($_GET['page'])
												$parameters .= '&amp;page='.$_GET['page']; 
											if ($_GET['paging'])
												$parameters .= '&amp;paging='.$_GET['paging']; 
											if ($_GET['sorting'])
												$parameters .= '&amp;sorting='.$_GET['sorting'];  
											if ($_GET['type'])
												$parameters .= '&amp;type='.$_GET['type']; 	 					
											echo '<a href="'.href_link(FILENAME_COUPON_ADMIN, 'cid='.(int)$cc_list->fields['coupon_id']).$parameters.'">'.image(DIR_WS_IMAGES.'icon_info.gif', IMAGE_ICON_INFO).'</a>'; 
										} 
										?>
									</td>
					            </tr>
								<?php $cc_list->MoveNext(); } ?>
          						<tr>
            						<td colspan="5">
										<table border="0" width="100%" cellspacing="0" cellpadding="2">
											<?php
												$parameters_for_page .= 'status='.$_GET['status'];
												if ($_GET['search']) { 
													$parameters_for_page .= '&search='.$_GET['search']; 
												}
												if ($_GET['page']) { 
													$parameters_for_page .= '&paging='.$_GET['page']; 
												}
												if ($_GET['sorting']) { 
													$parameters_for_page .= '&sorting='.$_GET['sorting']; 
												}			 
												if ($_GET['type']) { 
													$parameters_for_page .= '&type='.$_GET['type']; 
												}			 					
											?>
              								<tr class="page_break">
												<td class="smallText" valign="top" width="48%"><?php echo 'Angezeigt werden '.$query['from'].' bis '.$query['to'].' (von insgesamt '.$query['total'].' Gutscheine)'; ?></td>
												<td class="smallText" align="right" width="4%">&nbsp;</td>
												<td class="smallText" align="right" width="48%">
													Gutscheine pro Seite: <?php echo $query['page_break']->perSiteDropdown($_GET['page']); ?>
												</td>
											</tr>
											<tr><td align="center" colspan="2"><?php echo '<br />'.$query['links']; ?></td></tr>
              								
            							</table>
									</td>
          						</tr>
        					</table>
						</td>
				<?php
				    $heading = array();
				    $contents = array();
				
				    switch ($_GET['action']) {
					    case 'voucherdelete':
						    $heading[] = array('text' => '['.(int)$_GET['cid'].'] '.DELETE_NOW);
							$contents[] = array('text' => TEXT_CONFIRM_DELETE.'<br /><br />'.'<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', 'action=confirmdelete&cid='.(int)$_GET['cid'].$parameters, 'NONSSL').'">'.BUTTON_CONFIRM.'</a>'.'<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', 'cid='.$cInfo->coupon_id.$parameters, 'NONSSL').'">'.BUTTON_CANCEL.'</a>');
				    	break;
				
					    case 'deleteinactive':
						    $heading[] = array('text' => TEXT_COUPON_INACTIVE.' '.DELETE_NOW);
						    $contents[] = array('text' => TEXT_CONFIRM_DELETE_INACTIVE.'<br /><br />'.'<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php','action=confirmdeleteinactive', 'NONSSL').'">'.BUTTON_CONFIRM.'</a>'.'<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', '', 'NONSSL').'">'.BUTTON_CANCEL.'</a>');
				    	break;
				
					    default:
							if (empty($cInfo->coupon_code)){
				
							    $heading[] = array('text'=> ' ');
								$contents_temp = '';
								$contents_temp .= ERROR_NO_COUPONS_EXIST.'<br /><br />';
								$contents_temp .= '<center>';
								$contents_temp .= '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', '&amp;action=new').'">'.BUTTON_INSERT.'</a>';	
								$contents_temp .= '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php', '').'">'.BUTTON_CANCEL.'</a>';	
								$contents_temp .= '</center>';
								
								$contents[] = array('text' => $contents_temp);
				
							} else {
						
						  	  $heading[] = array('text'=>'['.$cInfo->coupon_id.'] - CODE : '.$cInfo->coupon_code);
								$contents_temp = '';
						
								$coupon_name = $db->db_query("SELECT coupon_name FROM ".TABLE_COUPONS_DESCRIPTION." WHERE coupon_id = '".$cInfo->coupon_id."' AND language_id = '".(int)$_SESSION['languages_id']."'");
								if ($coupon_name->fields['coupon_name'] != '')
									$contents_temp .= COUPON_NAME.' : '.$coupon_name->fields['coupon_name'].'<br />';
				
							    $amount = $cInfo->coupon_amount;
								if ($cInfo->coupon_type == "G") {
									$coupon_type = TYPE_G;
									$amount = $currencies->format($amount);
								} else if ($cInfo->coupon_type == "P") {
									$coupon_type = TYPE_P;			
									$amount .= ' %';
								} else if ($cInfo->coupon_type == "F") {
									$coupon_type = TYPE_F;
									$amount = $currencies->format($amount);
								} else if ($cInfo->coupon_type == "S") {
									$coupon_type = TYPE_S;
									$amount = TEXT_FREE_SHIPPING;
								}
								$contents_temp .= COUPON_TYPE.' : '.$coupon_type.'<br />';
								$contents_temp .= COUPON_AMOUNT.' : '.$amount.'<br />';
								$contents_temp .= COUPON_CODE.' : '.$cInfo->coupon_code.'<br /><br />';			
							
								if ($cInfo->coupon_type != "G") {
				
									$contents_temp .= COUPON_STARTDATE.' : '.date_short($cInfo->coupon_start_date).'<br />';
									$contents_temp .= COUPON_FINISHDATE.' : '.date_short($cInfo->coupon_expire_date).'<br /><br />';
				
									$contents_temp .= COUPON_USES_COUPON.' : '.$cInfo->uses_per_coupon.'<br />';
									$contents_temp .= COUPON_USES_USER.' : '.$cInfo->uses_per_user.'<br /><br />';
					
						        	$prod_details = NONE;
								    if ($cInfo->restrict_to_products) {
				    	    	  		$prod_details = '<a href="listproducts.php?cid='.$cInfo->coupon_id.'" target="_blank" onclick="window.open(\'listproducts.php?cid='.$cInfo->coupon_id.'\', \'Valid_Categories\', \'scrollbars=yes, resizable=yes, menubar=yes, width=600, height=600\'); return false;">'.VIEW.'</a>';
				        			}	     
									$contents_temp .= COUPON_PRODUCTS.' : '.$prod_details.'<br />';			      	
									$cat_details = NONE;
								    if ($cInfo->restrict_to_categories) {
				    	    			$cat_details = '<a href="listcategories.php?cid='.$cInfo->coupon_id.'" target="_blank" ONCLICK="window.open(\'listcategories.php?cid='.$cInfo->coupon_id.'\', \'Valid_Categories\', \'scrollbars=yes, resizable=yes, menubar=yes, width=600, height=600\'); return false;">'.VIEW .'</a>';
				        			}
									$contents_temp .= COUPON_CATEGORIES.' : '.$cat_details.'<br /><br />';
								}
				
								$contents_temp .= DATE_CREATED.' : '.date_short($cInfo->date_created).'<br />';
								
								if (date_short($cInfo->date_modified) != '')
									$contents_temp .= DATE_MODIFIED.' : '.date_short($cInfo->date_modified).'<br /><br />';
								else
									$contents_temp .= '<br />';
				
								$contents_temp .= '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php','action=email&cid='.$cInfo->coupon_id. $parameters,'NONSSL').'">'.BUTTON_EMAIL.'</a>';
								$contents_temp .= '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php','action=voucherreport&cid='.$cInfo->coupon_id. $parameters,'NONSSL').'">'.BUTTON_REPORT.'</a><br />';
								$contents_temp .= '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php','status=*&action=voucherdelete&cid='.$cInfo->coupon_id. $parameters,'NONSSL').'">'.BUTTON_DELETE.'</a>';
								$contents_temp .= '<a class="button" onclick="this.blur();" href="'.href_link('coupon_admin.php','action=voucheredit&cid='.$cInfo->coupon_id. $parameters,'NONSSL').'">'.BUTTON_EDIT.'</a>';
				
					        	$contents[] = array('text' => $contents_temp);
							
							}
				
				        break;
				    }
					?>                       
					<td width="25%" valign="top" class="border">
					<?php
						$box = new box;
						echo $box->infoBox($heading, $contents);
						echo '</td>'."\n";
					}
					?>			
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>